package com.znsb.msfq.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.H1ShopItemBean;
import com.znsb.msfq.utils.ActivityUtil;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.NetworkAddress;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class FeedBackActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.feed_edit_title)
    EditText feedEditTitle;
    @Bind(R.id.feed_edit_conent)
    EditText feedEditConent;
    @Bind(R.id.feed_tv_num)
    TextView feedTvNum;
    @Bind(R.id.feed_tv_commit)
    TextView feedTvCommit;

    @Override
    protected int getlayoutView() {
        return R.layout.act_feed_back;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("意见反馈");
    }

    @Override
    protected void initData() {
        InputFilter[] emojiFilters = {emojiFilter};
        feedEditTitle.setFilters(emojiFilters);
        feedEditConent.setFilters(emojiFilters);
        feedEditConent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                feedTvNum.setText(feedEditConent.getText().toString().trim().length() + "/150");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //用户意见反馈
    void GetAddFeedback(Object title, Object message) {

        Map<String, Object> maps = new HashMap<>();
        maps.put("title", title);//意见反馈标题
        maps.put("message", message);//意见内容
        HttpUtils.getInstance(this).postAsnyRequest(NetworkAddress.addFeedback, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String code, JSONObject request) {
                LogUtils.i("", ">>>>>>RecommendGoods" + request);
                ToastUtils.TextToast(ZnsbApp.mContext, getResources().getString(R.string.refer), 2000);
                ActivityUtil.goBack(FeedBackActivity.this);

            }

            @Override
            public void onFailure(String result) {
                LogUtils.i("", ">>>>>>onFailure" + result);
            }
        });


    }


    private static InputFilter emojiFilter = new InputFilter() {


        Pattern emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                   int dstart,
                                   int dend) {
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                return "";
            }
            return null;
        }
    };


    @Override
    protected void clickListener(View view) {

    }

    @OnClick({R.id.bar_img_back, R.id.feed_tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.feed_tv_commit:
                String title = feedEditTitle.getText().toString().trim();
                String message = feedEditConent.getText().toString().trim();
                if (title.length() > 0 && message.length() > 0) {
                    GetAddFeedback(title, message);
                } else {
                    ToastUtils.TextToast(FeedBackActivity.this, "内容不能为空", 3000);
                }

                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
